<template>
  <el-dialog :visible.sync="visible" title="应用设置" width="600px">
    <el-form :disabled="appInfo.id!=null&&!appInfo.adminUser" :model="appInfo" label-width="100px"
             size="mini"
             style="width: 400px;margin-left: 50px">
      <el-form-item label="应用名称">
        <el-input v-model="appInfo.appName" :disabled="appInfo.id!=null"/>
      </el-form-item>
      <el-form-item label="展示名称">
        <el-input v-model="appInfo.appShowName"></el-input>
      </el-form-item>
      <el-form-item label="构建方式">
        <el-radio-group v-model="appInfo.buildType">
          <el-radio v-for="t in buildTypes" :key="t.code" :label="t.code">{{ t.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="appInfo.buildType=='JENKINS'" label="代码地址">
        <el-input v-model="buildSetting.gitUrl"></el-input>
      </el-form-item>
      <el-form-item v-if="appInfo.buildType=='JENKINS'" label="制品文件">
        <el-input v-model="buildSetting.artifactFile"></el-input>
      </el-form-item>
      <el-form-item label="部署方式">
        <el-radio-group v-model="appInfo.deployType">
          <el-radio v-for="t in deployTypes" :key="t.code" :label="t.code">{{ t.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="日志方式">
        <el-radio-group v-model="appInfo.logType">
          <el-radio v-for="t in logTypes" :key="t.code" :label="t.code">{{ t.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="appInfo.logType!='NONE'" label="日志目录">
        <el-input v-model="logSetting.rootPath"></el-input>
      </el-form-item>
      <el-form-item v-if="appInfo.logType!='NONE'" label="日志文件">
        <el-input v-model="logSetting.fileName"></el-input>
      </el-form-item>
      <el-form-item label="监控方式">
        <el-radio-group v-model="appInfo.monitorType">
          <el-radio v-for="t in monitorTypes" :key="t.code" :label="t.code">{{ t.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="appInfo.monitorType=='SPRING_ACTUATOR'" label="监控协议">
        <el-radio-group v-model="monitorSetting.actuateType">
          <el-radio label="web">Web</el-radio>
          <el-radio disabled label="jmx">Jmx</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="appInfo.monitorType=='SPRING_ACTUATOR'" label="服务注册监控">
        <el-switch v-model="monitorSetting.serviceRegistry"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveApp(appInfo)">保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {appInfoApi, saveAppApi} from "@/api/manage";
import {enumsApi} from "@/api/common";

export default {
  data() {
    return {
      visible: false,
      appInfo: {},
      buildSetting: {},
      monitorSetting: {},
      logSetting: {},
      buildTypes: [],
      deployTypes: [],
      logTypes: [],
      monitorTypes: [],
    };
  },
  methods: {
    async open(appName) {
      this.appInfo = {};
      this.logSetting = {};
      this.buildSetting = {};
      this.monitorSetting = {};
      if (appName) {
        let appInfo = await appInfoApi(appName);
        this.appInfo = appInfo;
        this.appInfo.buildType = appInfo.buildType ? appInfo.buildType.code : 'NONE';
        this.appInfo.deployType = appInfo.deployType ? appInfo.deployType.code : 'NONE';
        this.appInfo.logType = appInfo.logType ? appInfo.logType.code : 'NONE';
        this.appInfo.monitorType = appInfo.monitorType ? appInfo.monitorType.code : 'NONE';
        this.buildSetting = appInfo.buildSetting ? appInfo.buildSetting : {};
        this.logSetting = appInfo.logSetting ? appInfo.logSetting : {};
        this.monitorSetting = appInfo.monitorSetting ? appInfo.monitorSetting : {};
      }
      this.visible = true;
    },
    async saveApp() {
      if (this.appInfo.buildType == 'JENKINS') {
        this.appInfo.buildSetting = this.buildSetting;
      }
      if (this.appInfo.logType != 'NONE') {
        this.appInfo.logSetting = this.logSetting;
      }
      if (this.appInfo.monitorType == 'SPRING_ACTUATOR') {
        this.appInfo.monitorSetting = this.monitorSetting;
      }
      await saveAppApi(this.appInfo);
      this.$message({message: `保存成功`, type: "success",});
      this.$emit("saveSuccess");
      this.visible = false;
    }
  },
  async created() {
    this.buildTypes = await enumsApi("AppBuildType");
    this.deployTypes = await enumsApi("AppDeployType");
    this.logTypes = await enumsApi("AppLogType");
    this.monitorTypes = await enumsApi("AppMonitorType");
  },
};
</script>